<?php
require '../model/Db.php';
$Db = Db::getInstance();
$admin_data = $Db->isadmin();

function job($job){
	if($job == 3){
		$job = '站务';
	}elseif($job == 2){
		$job = '区务';
	}elseif($job == 1){
		$job = '版务';
	}else{
		$job = '普通用户';
	}
	return $job;
}
function sex($sex){
	if($sex == '1'){
		$sex = '男';
	}else{
		$sex = '女';
	}
	return $sex;
}
if(!$admin_data){
	$user_data = $Db->islogin();
	//身份
	$job = job($user_data['job']);
	if($user_data['job'] == 3){
		$admin_data = $user_data;
	}
}else{
	$job = 'root';
}
//问候语
if(date('H:i:s')>'12:00:00'){
	$helo = "下午好";
}else{
	$helo = "早上好";
}
if($admin_data || $user_data['job'] != 0){
    
	switch ($_GET['p']) {
		case 0:
		if($_GET['clear'] == 'on'){
			$table_name = $Db->query('select table_name from information_schema.tables where table_schema in ("bbs") and data_free>0');
			$tables = '';
			foreach ($table_name as $table_v) {
				$tables .= $table_v['table_name'].',';
			}
			$tables = trim($tables,',');
			if($tables){
				$Db->query("optimize table {$tables}");
			}
			$msg = $tables?'清理成功！':'没有碎片垃圾可清理！';
			$class = 'ok';
			$reurl = 'admin.php';
			include './view/skip.html';
			exit();
		}
			$self = [
			title => '系统信息'
		];
			$mysql_v = $Db->query('select version() as v')[0]['v'];
			$zone_cnt = $Db->count([
				table => ['zone']
			]);
			$module_cnt = $Db->count([
				table => ['module']
			]);
			$cont_cnt = $Db->count([
				table => ['content'],
				where => 'delete_time is NULL'
			]);
			$user_cnt = $Db->count([
				table => ['bbs_user'],
				where => 'id > 0'
			]);
			$man_cnt = $Db->count([
				table => ['bbs_user'],
				where => 'job <> "0"'
			]);
			$man_cnt += $Db->count([
				table => ['bbs_manager']
			]);
			$now = time()-1800;
			$online_cnt = $Db->count([
				table => ['bbs_user'],
				where => "last_time > {$now}"
			]);
			//碎片信息
			$sum = $Db->query('select sum(data_free) sum from information_schema.tables where table_schema in ("bbs") and data_free>0')[0]['sum'];
			break;
		
		case 1:
			$self = [
			title => '管理员',
			css => '../public/css/admin-table.css'
		];
			$u_data = $Db->find([
				table => ['bbs_user'],
				field => 'id,user,job,phone',
				where => 'job <> "0"'
			]);
			$man_data = $Db->find([
				table => ['bbs_manager'],
				field => 'user,phone'
			]);
			$all_data = [];
			foreach ($man_data as $man_item) {
				$man_item['job_name'] = 'root';
				$all_data[] = $man_item;
			}
			foreach ($u_data as $u_item) {
				$u_item['job_name'] = job($u_item['job']);
				$all_data[] = $u_item;
			}
			$all_data = $Db->tohtml($all_data);
			break;
		
		case 2:
			require '../model/Page.php';
			$self = [
			title => '用户列表',
			css => '../public/css/admin-table.css'
		];
			$count = $Db->count([
				table => ['bbs_user']
			]);
			$page = page($count,30,5);
			$u_data = $Db->find([
				table => ['bbs_user'],
				field => 'id,user,job,sex,email,phone,last_time',
				where => "id>0 {$page['limit']}"
			]);
			$all_data = [];
			foreach ($u_data as $u_item) {
				$u_item['job_name'] = job($u_item['job']);
				$u_item['sex'] = sex($u_item['sex']);
				$all_data[] = $u_item;
			}
			$all_data = $Db->tohtml($all_data);
			break;

		case 3:
			$self = [
			title => '主版面管理',
			css => '../public/css/admin-cont.css'
		];
			$zone_list = [];
			$zone_data = $Db->find([
				table => ['zone'],
				where => '1 order by sort,id'
			]);
			foreach ($zone_data as $zone_item) {
				$uids = json_decode($zone_item['manager'])->uids;
				$uids = implode(',', $uids);
				$u_data = $Db->find([
					table => ['bbs_user'],
					field => 'id,user',
					where => "id in ({$uids}) "
				]);
				$zone_item['manager'] = [];
				if($u_data){
					foreach ($u_data as $u_item) {
						$zone_item['manager'][] = $u_item;
					}
				}
				
				$zone_list[] = $zone_item;
			}
			$zone_list = $Db->tohtml($zone_list);
			break;

		case 4:
			$self = [
			title => '从版面管理',
			css => '../public/css/admin-cont.css'
		];
			if($admin_data){
				$where = '1 order by sort,id';
			}else{
				$zids_arr = json_decode($user_data['follow_me'])->zids;
				$mids_arr = json_decode($user_data['follow_me'])->mids;
				$mids = implode(',', $mids_arr);
				$z_data = $Db->find([
					table => ['module'],
					field => 'zid',
					where => "id in ({$mids})"
				]);
	
				foreach ($z_data as $z_item) {

					if(!in_array($z_item['zid'], $zids_arr)){
						array_push($zids_arr, $z_item['zid']);
					}
				}
				$zids = implode(',', $zids_arr);

				$where = "id in ({$zids}) order by sort,id";
			}
			$zone_data = $Db->find([
				table => ['zone'],
				where => $where
			]);
			break;
		
		case 5:
		
			$self = [
			title => '帖子管理',
			css => '../public/css/admin-cont.css'
		];
			$zone_data = $Db->find([
				table => ['zone'],
				field => 'name,id',
				where => '1 order by sort,id'
			]);
			$list = [];
			foreach ($zone_data as $zone_item) {
				$zone_item['module'] = $Db->find([
					table => ['module'],
					field => 'name,id',
					where => "zid = {$zone_item['id']}"
				]);
				$list[] = $zone_item;
			}
			$list = $Db->tohtml($list);
			break;

		case 6:
		$self['css'] = '../public/css/admin-cont.css';
		$self['title'] = $_GET['q']==2?'修改从版面':'修改主版面';
		$name = $_GET['q']==2?'从版面':'主版面';
		if(isset($_POST['submit'])){
			$_POST = $Db->escape($_POST);
			if($_GET['q']==2){
				$Db->update([
					table => 'module',
					map => [
						kid => $_POST['zone'],
						name => $_POST['name'],
						info => $_POST['info'],
						description => $_POST['desc'],
						notice => $_POST['notice'],
					],
					where => "id = {$_GET['id']}"
				]);
				$reurl = '?p=4';
			}else{
				$Db->update([
					table => 'zone',
					map => [
						name => $_POST['name'],
					],
					where => "id = {$_GET['id']}"
				]);
				$reurl = '?p=3';
			}

			$msg = '修改成功！';
			$class = 'ok';
			include './view/skip.html';
			exit();
		}
		$table = $_GET['q']==2?'module':'zone';
		$exist = $Db->find([
			table => [$table],
			where => "id = {$_GET['id']}"
		])[0];
		if(!$exist){
			$p = $_GET['q']==2?4:3;
			$msg = '页面不存在！';
			$class = 'err';
			$reurl = '?p='.$p;
			include './view/skip.html';
			exit();
		}
		if($_GET['q'] == 2){
			$kind_list = $Db->find([
				table => ['kind'],
				where => "id = 0 or zid = {$exist['zid']} order by sort,id"
			]);
		}
			break;
	

		case 7:
		$self = [
			title => '被举报的帖子',
			css => '../public/css/admin-cont.css'
		];
		$list = $Db->query('select distinct a.cid from report a,content b where a.cid = b.id and a.delete_time is NULL and b.delete_time is NULL');

			break;

		case 8:
		$self['css'] = '../public/css/admin-cont.css';
		$self['title'] = '修改管理权限';
			$exist = $Db->find([
				table => ['bbs_user'],
				where => "id = {$_GET['id']}"
			])[0];
			if(!$exist){
				$msg = '用户不存在！';
				$class = 'err';
				$reurl = '?p=2';
				include './view/skip.html';
				exit();
			}
			if($_GET['id'] == $user_data['id'] || ($user_data && $exist['job']>=$user_data['job'])){
				$msg = '没有操作权限！';
				$class = 'err';
				$reurl = '?p=2';
				include './view/skip.html';
				exit();
			}
			if($user_data){
				$my_zids = json_decode($user_data['follow_me'])->zids;
				$myzids = implode(',', $my_zids);
			}
			$zids_arr = json_decode($exist['follow_me'])->zids;
			$mids_arr = json_decode($exist['follow_me'])->mids;
			$zids = implode(',', $zids_arr);
			$mids = implode(',', $mids_arr);
			if(!$zids){
				$zids = -1;
			}
			if(!$mids){
				$mids = -1;
			}

			//未管理列表
			if($admin_data){
				$z_list = $Db->find([
					table => ['zone'],
					field => 'id,name',
					where => "id NOT IN ({$zids})"
				]);
			}
			$m_where = $admin_data ? '':"and zid in ({$myzids})";
			$m_list = $Db->find([
				table => ['module'],
				field => 'id,name',
				where => "id NOT IN ({$mids}) {$m_where}"
			]);

			//已管理列表
			$zone_list = $Db->find([
				table => ['zone'],
				field => 'id,name',
				where => "id in ({$zids})"
			]);
			$module_list = $Db->find([
				table => ['module'],
				field => 'id,name,zid',
				where => "id in ({$mids})"
			]);
			$list = [zone=>[],module=>[]];
			if($zone_list){
				foreach ($zone_list as $zone_v) {
					$list['zone'][] = $zone_v;
				}
			}
			if($module_list){
				foreach ($module_list as $module_v) {
					$list['module'][] = $module_v;
				}
			}
			
			$exist = $Db->tohtml($exist);
			break;
		case 9: 
		    $self = [
    			title => '故障维修管理',
    			css => '../public/css/admin-cont.css'
    		];
		   
		    break;
		    
	    case 10:
	        $self = [
    			title => '活动申报管理',
    			css => '../public/css/admin-cont.css'
    		];
    		
	       break;
	       
	    case 11:
	        $self = [
    			title => '敏感词设置'
    		];
    		$path = $_SERVER['DOCUMENT_ROOT'].'/nsbbs/public/invalidword.txt';
    		$invalidwordstr = file_get_contents($path);
    		if(isset($_POST['submit'])){
    		    file_put_contents($path,$_POST['invalid']);
    		    $invalidwordstr = $_POST['invalid'];
    		}
    		
	       break;
	}
	
}else{
	$msg = '你还没有登录呢~';
	$class = 'err';
	$reurl = 'login.php';
	include './view/skip.html';
	exit();
}
$Db->close();
	$css = [
	'../public/css/admin-public.css',
	$self['css']
];
include './view/header.html';
switch ($_GET['p']) {
	case 0:
		include './view/index.html';
		break;
		
	case 1:
		include './view/manager.html';
		break;
	
	case 2:
		include './view/userlist.html';
		break;

	case 3:
		include './view/zone.html';
		break;

	case 4:
		include './view/module.html';
		break;
	
	case 5:
		include './view/content.html';
		break;

	case 6:
		include './view/update.html';
		break;

	case 7:
		include './view/report.html';
		break;

	case 8:
		include './view/ac_manager.html';
		break;
	
	case 9:
		include './view/gzwx.html';
		break;
		
	case 10:
		include './view/hdsb.html';
		break;
	
	case 11:
		include './view/invalidword.html';
		break;
}